#coding = utf-8
from selenium.common.exceptions import NoSuchElementException,TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from UI自动化基础框架 import setting
class BasePage(object):
    def __init__(self,driver,url):
        self.__driver = driver
        self.__url = url

    def open(self):
        self.__driver.get(url=self.__url)

    def find_element(self,*locator,timeout=None):
        try:
            return self._init_wait(timeout).until(EC.visibility_of_element_located(locator=locator))
        except (NoSuchElementException,TimeoutException):
            self.__driver.quit()
            raise TimeoutException(msg="定位元素失败，具体为{}".format(locator))

    def send_keys(self,webElement,keys):
        webElement.clear()   #先清空，避免元素干扰
        webElement.send_keys(keys)

    def _init_wait(self,timeout):
        if timeout is None:
            return WebDriverWait(driver=self.__driver,timeout=setting.MaxTimeout)
        else:
            return WebDriverWait(driver=self.__driver,timeout=timeout)